package com.gzhryc.net_marketing.cloud.goods.services;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.config.DefaultEnum;
import com.gzhryc.common.data.TreeUtils;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.goods.dao.GoodsTypeDao;
import com.gzhryc.net_marketing.cloud.goods.dao.db.GoodsType;
import com.gzhryc.net_marketing.cloud.goods.services.dto.GoodsTypeSearch;
import com.gzhryc.net_marketing.core.models.GoodsTypeItem;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class GoodsTypeService {

    static Logger log = Logger.getLogger(GoodsTypeService.class);

    public static GoodsTypeService self() {
        return MultiDBTools.getService(MarketConstants.JDBC_KEY,GoodsTypeService.class, true);
    }

    GoodsTypeDao dao;

    public GoodsTypeService(String jdbcKey) {
        this.dao = new GoodsTypeDao(jdbcKey);
    }

    public List<GoodsType> findBySearch(GoodsTypeSearch search, Integer pageNo, Integer pageLimit){
        Conditions condition = dao.buildConditions(search);
        condition.asc("priority");
        try {
            return dao.findByCondition(condition,pageNo, pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(GoodsTypeSearch search){
        Conditions condition = dao.buildConditions(search);
        try {
            return dao.count(condition);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<GoodsType> findByParentId(Long parentId){
        if(parentId != null && parentId > 0){
            Conditions condition = new Conditions(GoodsType.class);
            condition.and().is("parentId",parentId);
            condition.asc("priority");
            try {
                return dao.findByCondition(condition);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }else{
            Conditions condition = new Conditions(GoodsType.class);
            condition.and().isNull("parentId").or().is("parentId",0);
            condition.asc("priority");
            try {
                return dao.findByCondition(condition);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<GoodsType> findAllUsable(){
        Conditions condition = new Conditions(GoodsType.class);
        condition.and().is("state",DefaultEnum.Status.Usable.index());
        condition.asc("priority");
        condition.asc("id");
        try {
            return dao.findByCondition(condition);
        } catch (SQLException var4) {
            var4.printStackTrace();
        }
        return null;
    }

    public List<GoodsType> findAllTree() {
        Conditions condition = new Conditions(GoodsType.class);
        condition.and().is("state",DefaultEnum.Status.Usable.index());
        condition.asc("priority");
        condition.asc("id");

        try {
            List<GoodsType> data = dao.findByCondition(condition);
            if (data != null) {
                return TreeUtils.byTree(data);
            }
        } catch (SQLException var4) {
            var4.printStackTrace();
        }
        return null;
    }

    public GoodsType getById(Long id){
        if(id != null){
            try {
                return dao.getById(id);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<GoodsType> findByIds(String[] ids){
        if(ids != null && ids.length > 0){
            Conditions condition = new Conditions(GoodsType.class);
            condition.and().in("id",ids);
            try {
                return dao.findByCondition(condition);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<GoodsType> findByCodes(String[] codes){
        if(codes != null && codes.length > 0){
            Conditions condition = new Conditions(GoodsType.class);
            condition.and().in("code",codes);
            try {
                return dao.findByCondition(condition);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public GoodsType getJoinById(Long id){
        if(id != null){
            try {
                return dao.getJoinById(id);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(GoodsType goodsType) throws LogicException {
        if(goodsType.getParentId() != null){
            GoodsType parent = GoodsTypeService.self().getById(goodsType.getParentId());
            if(parent != null){
                goodsType.setClassify(parent.getClassify());
            }else{
                throw new LogicException("未找到上级商品类型");
            }
        }

        if(goodsType.getPriority() == null){
            goodsType.setPriority(10);
        }
        if(goodsType.getState() == null){
            goodsType.setState(DefaultEnum.Status.Usable.index());
        }
        goodsType.setCreateDate(new Date());
        try {
            return dao.insert(goodsType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(GoodsType goodsType) throws LogicException {
        try {
            return dao.updateNotNull(goodsType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        if(id != null){
            try {
                return dao.deleteById(id) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public static class TreeConvertListen implements TreeUtils.ConvertListen<GoodsType, GoodsTypeItem> {

        @Override
        public GoodsTypeItem change(GoodsType parent, GoodsType obj) {
            GoodsTypeItem item = ClassTools.copy(new GoodsTypeItem(),obj);
            return item;
        }
    }
}
